package com.youbug.system.service.impl;

import com.youbug.system.entity.Article;
import com.youbug.system.mapper.ArticleMapper;
import com.youbug.system.service.IArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import com.youbug.common.vo.QueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 文章管理 服务实现类
 * </p>
 *
 * @author youbug
 * @since 2024-03-03
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {

        @Override
        public IPage<Article> getArticlePage(QueryVo<Article> queryVo) {

        Page<Article> paramPage = new Page<>();

        paramPage.setCurrent(queryVo.getCurrentPage());

        paramPage.setSize(queryVo.getPageSize());

        QueryWrapper<Article> wrapper = new QueryWrapper<>();

        Article article = queryVo.getPayload();

        if(!ObjectUtils.isEmpty(article)){
            if(StringUtils.hasText(article.getTitle())){
                wrapper.eq("title", article.getTitle());
            }
            if(StringUtils.hasText(article.getContent())){
                wrapper.eq("content", article.getContent());
            }
            if(StringUtils.hasText(article.getCoverImg())){
                wrapper.eq("cover_img", article.getCoverImg());
            }
        }
        if(!ObjectUtils.isEmpty(queryVo.getStartTime())){
            wrapper.gt("create_time", queryVo.getStartTime());
        }
        if(!ObjectUtils.isEmpty(queryVo.getEndTime())){
            wrapper.lt("create_time", queryVo.getEndTime());
        }
        wrapper.orderByDesc("create_time");

        Page<Article> page = this.page(paramPage, wrapper);

        return page;
        }

}
